package top.api.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.api.common.ResponseResult;
import top.api.domain.dto.AddCategoryDto;
import top.api.domain.dto.CategoryUpdateDto;
import top.api.domain.entity.Category;
import top.api.domain.vo.ExcelCategoryVo;
import top.api.enums.AppHttpCodeEnum;
import top.api.service.CategoryService;
import top.api.utils.BeanCopyUtils;
import top.api.utils.WebUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
@Api(tags = "分类列表")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/listAllCategory")
    @ApiOperation(value = "返回所有分类列表")
    public ResponseResult listAllCategory(){
        return categoryService.listAllCategory();
    }

    @GetMapping("/export")
    @PreAuthorize("@ps.hasPermission('content:category:export')") // 权限校验
    @ApiOperation(value = "导出Excel")
    public void dowExport(HttpServletResponse response){
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            //获取需要导出的数据
            List<Category> categoryVos = categoryService.list();

            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);

        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "分类分页查询")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult getCategoryListPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,String name,String status){
        return categoryService.getCategoryListPage(pageNum,pageSize,name,status);
    }

    @PostMapping
    @ApiOperation(value = "添加分类")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult addCategory(@RequestBody AddCategoryDto addCategoryDto){
        return categoryService.addCategory(addCategoryDto);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询分类详情")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult getCategoryInfo(@PathVariable Long id){
        return categoryService.getCategoryInfo(id);
    }

    @PutMapping
    @ApiOperation(value = "更新分类")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult getCategoryInfo(@RequestBody CategoryUpdateDto categoryUpdateDto){
        return categoryService.updateCategory(categoryUpdateDto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除分类")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult deleteCategory(@PathVariable Long id){
        return categoryService.deleteCategory(id);
    }
}
